﻿
//#define DEBUG

using System;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;

namespace Setup
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.ThreadException += Application_ThreadException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            //string xmlPath = AppDomain.CurrentDomain.BaseDirectory + (args != null && args.Length > 0 ? args[0] : "setup.xml");

#if !DEBUG
            string temppath = Path.GetTempPath();
            if (AppDomain.CurrentDomain.BaseDirectory != temppath)
            {
                try
                {
                    //处理启动参数
                    //if (!string.IsNullOrEmpty(xmlPath) && File.Exists(xmlPath))
                    //    File.Copy(xmlPath, temppath + Path.GetFileName(xmlPath), true);
                    //else
                    //{
                    //    if (File.Exists(temppath + Path.GetFileName(xmlPath)))
                    //        File.Delete(temppath + Path.GetFileName(xmlPath));
                    //}

                    //关闭所有未完成的安装
                    ProcessLogic.CloseProcess(new string[] { "Setup" },closeThis:false);

                    //复制最新安装程序
                    string setupPath = temppath + "Setup.exe";
                    if (File.Exists(setupPath))
                        FileLogic.AddSecurityControll2File(setupPath);
                    File.Copy(Application.ExecutablePath, setupPath, true);

                    //添加最高权限和防火墙白名单
                    FileLogic.AddSecurityControll2File(setupPath);
                    FirewallLogic.FirewallAddRule(setupPath, true);

                    //设置启动参数 并启动安装程序
                    string param = "";
                    if (args != null && args.Length > 0)
                        foreach (var item in args)
                            param += " " + item;
                    System.Diagnostics.Process.Start(setupPath, param);
                    return;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
#endif

            //Logic.Update();
            Logic.Type = OperationType.Install;
            if (args != null && args.Length > 0)
            {
                foreach (var item in args)
                {
                    string type = item.ToLower();
                    switch (type)
                    {
                        case "update":
                            Logic.Type = OperationType.Update;
                            break;
                        case "uninstall":
                            Logic.Type = OperationType.UnInstall;
                            break;
                    }
                }
            }

            //Logic.Install = InstallInfo.Open(xmlPath);

            if (Logic.Install == null)
            {
                Logic.Install = Logic.DefaultInstallInfo();
                if (Logic.Install == null)
                {
                    MessageBox.Show("未找到适用与当前操作系统的安装文件,请联系管理人员！");
                    return;
                }
                //InstallInfo info = RegistryLogic.GetEexInfo(Logic.Install.SoftWareKey);
                //if (info != null)
                //{
                //    Logic.Install.InstallLocation = info.InstallLocation?.ToString();
                //    Logic.Install.UninstallString = info.UninstallString?.ToString();
                //    Logic.Install.DisplayName = info.DisplayName?.ToString();
                //    Logic.Install.DisplayVersion = info.DisplayVersion?.ToString();
                //}
            }


            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Application.Run(new Form1());
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            if (e.ExceptionObject is System.Net.WebException)
            {
                MessageBox.Show("网络异常，请检查网络连接是否正常并退出安装程序重试\r\n"+((Exception)e.ExceptionObject).Message);
            }
            else
            {
                MessageBox.Show(e.ExceptionObject.ToString());
            }

        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            MessageBox.Show(e.Exception.Message);
            MessageBox.Show(e.Exception.StackTrace);
        }
    }
}
